/*
 * Copyright 2017 hv0912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vimhe.ezimp2.handler.formatter;

import com.vimhe.ezimp2.exception.ImportException;
import com.vimhe.ezimp2.handler.Formatter;

import java.util.Map;

/**
 * 数据字典转换器
 * <p>把文本转换为数据库数据字典中对应的码值
 *
 * @author Vimhe
 */
public abstract class AbstractCodeFormatter implements Formatter<String> {

    @Override
    public String format(String cellValue, String formatterValue) {
        Map<String, String> code = getCodeMap().get(formatterValue);
        if (code == null) {
            throw new ImportException("发现数据字典中没有的代码【" + formatterValue + "】，无法进行转换");
        }

        return code.get(cellValue);
    }

    /**
     * 获取数据字典<br>
     * <pre>
     *     Map type, Map name, value  <br>
     *     Map 码值类型, Map 代码名称, 码值
     * </pre>
     *
     * @return 数据字典
     */
    protected abstract Map<String, Map<String, String>> getCodeMap();

}
